<template>
  <div class="user-avator-dropdown">
    <Dropdown @on-click="handleClick">
      <Badge :dot="!!messageUnreadCount">
        <Avatar :src="userAvator"/>
      </Badge>
      <Icon :size="18" type="md-arrow-dropdown"></Icon>
      <DropdownMenu slot="list">
        <DropdownItem name="message">
          消息中心<Badge style="margin-left: 10px" :count="messageUnreadCount"></Badge>
        </DropdownItem>
        <DropdownItem name="logout">退出登录</DropdownItem>
      </DropdownMenu>
    </Dropdown>
  </div>
</template>

<script>
import './user.less';
import { mapActions } from 'vuex';
import * as api from '@/api/api.js';
export default {
  name: 'User',
  props: {
    userAvator: {
      type: String,
      default: ''
    },
    messageUnreadCount: {
      type: Number,
      default: 0
    }
  },
  methods: {
    ...mapActions([
      'handleLogOut'
    ]),
    logout () {
//    this.handleLogOut().then(() => {
//      this.$router.push({
//        name: 'login'
//      })
//    })
			this.$Spin.show();
				api.getData('logout', this).then((res) => {
					if(res.state == 200) {
						this.$store.commit('setToken','');
						this.$store.commit('setAccess',[]);
						this.$router.push({
		          name: 'login'
		        })
					} else {
						this.$Notice.error({
							title: '提示',
							desc: res.msg,
						});
					}
					this.$Spin.hide();
				})
    },
    message () {
      this.$router.push({
        name: 'message_page'
      })
    },
    handleClick (name) {
      switch (name) {
        case 'logout': this.logout()
          break
        case 'message': this.message()
          break
      }
    }
  }
}
</script>
